package com.wiki.gds.user.provider.modular.sys.controller;


import com.wiki.gds.base.common.dto.ResponseDto;
import com.wiki.gds.base.common.exception.BizExceptionEnum;
import com.wiki.gds.base.common.utils.ResponseDtoFactory;
import com.wiki.gds.base.web.controller.support.HttpKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value = "TokenController", tags = "用户中心微服务-Token管理")
//@SessionAttributes("authorizationRequest")
public class TokenController {

    @Autowired
    private TokenStore tokenStore;


    @ApiImplicitParam(name = "Authorization", value = "请输入凭证", required = true, dataType = "String", paramType = "header")
    @ApiOperation(value = "注销token", notes = "注销token", httpMethod = "POST")
    @PostMapping("/token/revoke")
    public ResponseDto revokeToken() {
        ResponseDto response=ResponseDtoFactory.GetInstance().build(BizExceptionEnum.SUCCESSFUL_OPERATION);
        String header=  HttpKit.getRequest().getHeader("Authorization");
        if (header != null) {
            String tokenValue = header.replace("Bearer", "").trim();
            OAuth2AccessToken accessToken = tokenStore.readAccessToken(tokenValue);
            tokenStore.removeAccessToken(accessToken);
        }
        return response;
    }
    //仅展示  /oauth/token 会被spring oauth TokenEndpoint 自动获取
    @ApiOperation(value = "获取token", httpMethod = "POST", notes = "获取token")
    @PostMapping(value = "/oauth/token", produces = "application/json")
    public void authorize(
            @ApiParam(value = "oauth授权类型：authorization_code，password，client_credentials，refresh_token", required = true) @RequestParam String grant_type,
            @ApiParam(value = "客户端id", required = true) @RequestParam String client_id,
            @ApiParam(value = "客户端凭证码", required = true) @RequestParam String client_secret,
            @ApiParam(value = "用户名", required = true) @RequestParam String username,
            @ApiParam(value = "密码", required = true) @RequestParam String password,
            @ApiParam(value = "登录类型",defaultValue = "vc") @RequestParam String auth_type,
            @ApiParam(value = "图片验证码") @RequestParam String imgCode,
            @ApiParam(value = "作用域", required = true) @RequestParam String scope
    )
    {
    }

}
